package com.laizhenghua.share.device.controller;

import com.laizhenghua.share.common.core.domain.R;
import com.laizhenghua.share.common.core.web.controller.BaseController;
import com.laizhenghua.share.common.core.web.page.TableDataInfo;
import com.laizhenghua.share.common.core.web.page.TableSupport;
import com.laizhenghua.share.device.domain.entity.Cabinet;
import com.laizhenghua.share.device.service.ICabinetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * TODO
 *
 * @Description
 * @Author laizhenghua
 * @Date 2025/6/29 08:43
 **/
@Tag(name = "柜机API")
@RestController
@RequestMapping("/cabinet")
public class CabinetController extends BaseController {

    private final ICabinetService iCabinetService;

    @Autowired
    public CabinetController(ICabinetService iCabinetService) {
        this.iCabinetService = iCabinetService;
    }

    @PostMapping("/search-page/list")
    @Operation(summary = "分页查询")
    @Parameters({
            @Parameter(name = TableSupport.PAGE_NUM, description = "分页索引", example = "1"),
            @Parameter(name = TableSupport.PAGE_SIZE, description = "分页大小", example = "10")
    })
    @io.swagger.v3.oas.annotations.parameters.RequestBody(content = {@Content(schema = @Schema(implementation = Cabinet.class))})
    public R<TableDataInfo> searchPageList(@RequestBody Cabinet cabinet) {
        startPage();
        List<Cabinet> list = iCabinetService.searchPageList(cabinet);
        return R.ok(getDataTable(list));
    }

    @GetMapping("/search-no-use/{keyword}")
    @Operation(summary = "查询未使用的柜机")
    @Parameter(name = "keyword", description = "关键字", required = true)
    public R<List<Cabinet>> searchNoUse(@PathVariable String keyword) {
        return R.ok(iCabinetService.searchNoUse(keyword));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id获取详情")
    @Parameter(name = "id", description = "主键")
    public R<Cabinet> getDetailById(@PathVariable Long id) {
        return R.ok(iCabinetService.getById(id));
    }

    @Operation(summary = "保存")
    @PostMapping("/save")
    public R<Cabinet> save(@RequestBody Cabinet cabinet) {
        Cabinet s = iCabinetService.customSave(cabinet);
        return R.ok(s);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/delete/{ids}")
    public R<String> deleteByIds(@PathVariable Long[] ids) {
        boolean b = iCabinetService.removeBatchByIds(Arrays.asList(ids));
        return R.toR(b);
    }
}
